﻿using E_GovernmentSystem.Models;
using Microsoft.AspNetCore.Mvc;
using NuGet.Packaging.Signing;

namespace E_GovernmentSystem.Controllers
{
    /// <summary>
    /// 案件分类信息
    /// </summary>
    public class CaseClassificationController : Controller
    {

        /// <summary>
        /// 配置上下文类
        /// </summary>
        private readonly SqlSugar.ISqlSugarClient db;

        public CaseClassificationController(SqlSugar.ISqlSugarClient _db)
        {
            this.db = _db;
        }

        //案件分类信息主页
        public IActionResult Index()
        {

            return View();
        }

        //获取案件信息
        public ActionResult GetCaseClassificationList(string strName)
        {

            //获取案件全部信息
            List<caseclassificationtable> list = db.Queryable<caseclassificationtable>().ToList();
            List<CaseClassInfoTreeList> CaseList = new List<CaseClassInfoTreeList>();
            //利用组织机构名称进行条件查询
            if (!string.IsNullOrEmpty(strName))
            {
                list = list.Where(a => a.CaseTypeName != null && a.CaseTypeName.Contains(strName)).ToList();

                foreach (var item in list)
                {
                    CaseClassInfoTreeList treelist = new CaseClassInfoTreeList();
                    treelist.id = item.ClassificationId;
                    treelist.CaseTypeName = item.CaseTypeName;
                    treelist.CaseLevel = item.CaseLevel;
                    treelist.pid = item.CaseParentId;
                    treelist.EventPartIdentification = item.EventPartIdentification;
                    treelist.EventPartIdentificationId = item.EventPartIdentificationId;
                    list = db.Queryable<caseclassificationtable>().Where(a => a.CaseParentId == item.ClassificationId).ToList();
                    treelist.isParent = GetChildTreeList(list, item.ClassificationId).Count > 0 ? true : false;
                    treelist.children = BindTree(list, item.ClassificationId);
                    CaseList.Add(treelist);
                }
            }
            else
            {
                //在全部数据中筛选出有子节点的父级数据
                var childlist = GetChildTreeList(list, 0);
                foreach (var item in childlist)
                {
                    CaseClassInfoTreeList treelist = new CaseClassInfoTreeList();
                    treelist.id = item.ClassificationId;
                    treelist.CaseTypeName = item.CaseTypeName;
                    treelist.CaseLevel = item.CaseLevel;
                    treelist.pid = item.CaseParentId;
                    treelist.EventPartIdentification = item.EventPartIdentification;
                    treelist.EventPartIdentificationId = item.EventPartIdentificationId;
                    treelist.isParent = GetChildTreeList(list, item.ClassificationId).Count > 0 ? true : false;//查询当前数据是否含有子节点
                    treelist.children = BindTree(list, item.ClassificationId);//通过递归查询的方法查询出当前数据拥有的子节点对应的数据
                    CaseList.Add(treelist);
                }

            }

            var json = new
            {
                code = 0,
                data = CaseList,
                msg = "",
                count = CaseList.Count
            };

            return Json(json);
        }


        /// <summary>
        /// 检测数据中是否含有子节点，有就返回含有子节点的对应数据
        /// </summary>
        /// <param name="list">现有数据</param>
        /// <param name="ID">父级顶点的ID值</param>
        /// <returns></returns>
        public List<caseclassificationtable> GetChildTreeList(List<caseclassificationtable> list, int? ID)
        {
            var childlist = list.Where(a => a.CaseParentId == ID).ToList();
            return childlist;
        }
        /// <summary>
        /// 利用递归实现查询子节点数据
        /// </summary>
        /// <param name="list"></param>
        /// <param name="ID"></param>
        /// <returns></returns>
        public List<object> BindTree(List<caseclassificationtable> list, int? ID)
        {
            List<object> objtreelist = new List<object>();
            var childlist = GetChildTreeList(list, ID);//通过当前传入的ID查找该ID的数据是否含有子节点
            if (childlist.Count > 0)
            {
                foreach (var item in childlist)
                {
                    CaseClassInfoTreeList treelist = new CaseClassInfoTreeList();
                    treelist.id = item.ClassificationId;
                    treelist.CaseTypeName = item.CaseTypeName;
                    treelist.CaseLevel = item.CaseLevel;
                    treelist.pid = item.CaseParentId;
                    treelist.EventPartIdentification = item.EventPartIdentification;
                    treelist.EventPartIdentificationId = item.EventPartIdentificationId;
                    treelist.isParent = GetChildTreeList(list, item.ClassificationId).Count > 0 ? true : false;//查询当前数据是否含有子节点
                    treelist.children = BindTree(list, item.ClassificationId);//通过递归查询的方法查询出当前数据拥有的子节点对应的数据
                    objtreelist.Add(treelist);
                }
            }
            return objtreelist;
        }

        //保存  添加/编辑类别信息
        public IActionResult Save(caseclassificationtable info)
        {
            bool res = (db.Storageable(info).DefaultAddElseUpdate().ExecuteCommand()) > 0 ? true : false;

            return Json(res);
        }

        //绑定案件类别下拉框
        public IActionResult getCaseClassInfoBindSelect()
        {
            List<caseclassificationtable> list = db.Queryable<caseclassificationtable>().ToList(); ;
            return Json(list);
        }

        //删除
        public IActionResult Delete(int id)
        {
            bool res = false;
            string msg = string.Empty;
            List<caseclassificationtable> info = db.Queryable<caseclassificationtable>().Where(a => a.CaseParentId == id).ToList();

            if (info.Count == 0)
            {
                res = db.Deleteable<caseclassificationtable>(new caseclassificationtable { ClassificationId = id}).ExecuteCommand() > 0 ? true : false;
                msg = res ? "删除成功" : "删除失败";
            }
            else
            {
                res = false;
                msg = "该类型含有子节点，不能删除！！！";
            }
            var json = new
            {
                res = res,
                msg = msg
            };

            return Json(json);
        }
    }
}
